<?php
/**
 * Created by PhpStorm.
 * User: Silence
 * Date: 2017-07-03
 * Time: 15:37
 */

namespace App\Http\Controllers\Index;



use App\Base\BaseFunc;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;

class IndexController extends Controller
{
    public function index()
    {
        return view("index.index");
    }

    public function customerLogin(BaseFunc $baseFunc)
    {
        $input = Input::only('customer_phone');
        $password = Input::only('customer_password');
        $input['customer_password'] = md5($password['customer_password']);
        $customer = DB::table('customer')
            ->where('customer_phone',"=",$input['customer_phone'])
            ->value('customer_password');
        if($customer == NULL)
        {
            $baseFunc ->setRedirectMessage(false,"您还没有注册，请先注册",NULL,"/register");
        }
        else
        {
            $customerData = DB::table('customer')
                ->where('customer_phone',"=",$input['customer_phone'])
                ->where('customer_password',"=",$input['customer_password'])
                ->first();
            if($customerData == NULL)
            {
                $baseFunc ->setRedirectMessage(false,"密码错误",NULL,"/index");
            }
            else
            {
                session(['customer_id' => $customerData->customer_id]); //获取登录客户的ID
                session(['customer_name' => $customerData->customer_name]); //获取登录客户的名字
                session(['customer_phone' => $customerData->customer_phone]); //获取登录客户的电话
                session(['customer_area' => $customerData->customer_area]); //获取登录客户的区域ID

                $baseFunc ->setRedirectMessage(true,"登录成功，欢迎来到成都家政服务系统",NULL,"/start");
            }
        }
    }

    public function customerLogout(BaseFunc $baseFunc)
    {
        session(["customer_id"=>NULL]);
        $baseFunc ->setRedirectMessage(true,"您已退出登录",NULL,"/index");
    }

    public function customerRegister()
    {
        $data['area'] = DB::table('area') -> get();
        return view("index.register",$data);
    }

    public function customerRegisters(BaseFunc $baseFunc)
    {
        $data = Input::only('customer_name','customer_phone','customer_area');
        $password = Input::only('customer_password');
        $data['customer_password'] = md5($password['customer_password']);
        $check = DB::table('customer')
            ->where('customer_phone',"=",$data['customer_phone'])
            ->get();
        if($check == NULL)
        {
            DB::table('customer')->insert($data);
            $baseFunc->setRedirectMessage(true,"注册成功，请您登录",NULL,"/index");
        }
        else
        {
            $baseFunc->setRedirectMessage(false,"注册失败，此手机号已被注册",NULL,"/register");
        }
    }
}